package com.company.gxcpc2020;

import java.util.Arrays;
import java.util.Scanner;

public class GxcpcD {

    public static int n, m, k;
    public static int[][][] map;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            n = sc.nextInt();
            m = sc.nextInt();
            k = sc.nextInt();
            map = new int[2][n + 1][n + 1];
            for (int c = 0; c < 2; c++) {
                for (int i = 0; i <= n; i++) {
                    for (int j = 0; j <= n; j++) {
                        if (i == j){
                            continue;
                        }
                        map[c][i][j] = -1;
                    }
                }
            }
            for (int i = 0; i < m; i++) {
                int u = sc.nextInt(), v = sc.nextInt(), w = sc.nextInt(), c = sc.nextInt();
                map[c - 1][u][v] = w;
                map[c - 1][v][u] = w;
            }
            deepToString(map);
        }
    }

    public static void deepToString(int[][][] data) {
        for (int c = 0; c < 2; c++) {
            for (int i = 1; i <= n; i++) {
                System.out.println(Arrays.toString(Arrays.copyOfRange(data[c][i], 1, n + 1)));
            }
            System.out.println();
        }
    }
}
